const app = getApp()

const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatDate = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

const parseDate = date => {
    const regEx = new RegExp("\\-", "gi");
    date = date.replace(regEx, "/");
    return new Date(date);
}

//加一天
const addDay = date => {
    let newday = date.getTime() + 1 * 24 * 60 * 60 * 1000;
    newday = new Date(newday);
    return newday;
}

//加多月
const addMonths = function (v, n) {
    var t = new Date(v.getFullYear(), v.getMonth(), v.getDate(), v.getHours(), v.getMinutes(), v.getSeconds(), v.getMilliseconds());
    t.setMonth(v.getMonth() + n);
    if (t.getDate() != v.getDate()) {
        t.setDate(0);
    }
    return t;
}

function subscriptionsSetting() {
    wx.getSetting({
        withSubscriptions: true,
        success(res) {
            console.log('res.subscriptionsSetting')
            console.log(res.subscriptionsSetting)
            let itemSettings = res.subscriptionsSetting.itemSettings
            let templateIds = app.globalData.templateIds
            if (templateIds == null || templateIds.length == 0) {
                app.formPost('/app/wechat/template/list', null).then(res => {
                    if (res.code === 0) {
                        let templateIdList = res.data.list
                        for (let i = 0; i < templateIdList.length; i++) {
                            templateIds.push(templateIdList[i].templateId)
                        }
                        app.globalData.templateIds = templateIds
                        let tmplIds = app.globalData.templateIds
                        if (itemSettings) {
                            for (let i = 0; i < tmplIds.length; i++) {
                                if (itemSettings[tmplIds[i]] != 'accept') {
                                    tmplIds.pop();
                                }
                            }
                            if (tmplIds.length > 0) {
                                wx.requestSubscribeMessage({
                                    tmplIds: tmplIds,
                                    success(res) {
                                        console.log("订阅成功通知")
                                        console.log(res)
                                    }
                                })
                            }
                        } else {
                            wx.requestSubscribeMessage({
                                tmplIds: tmplIds,
                                success(res) {
                                    console.log("订阅成功通知")
                                    console.log(res)
                                }
                            })
                        }
                    }
                }).catch(e => {
                    app.message(e, 'error')
                })
            } else {
                // let tmplIds = ['4FGLukliNNxieBiizlW_6ctXIe8B6bgs-MidqaCWo2A', '4pYDMxKTagxGFkVGgghQxH06zB0IT7KaCZuYswza2_c', 'EXOwU2xWbItbLxu82gdaJLXkGhN3jDFDqJCOFBT-W9g']
                let tmplIds = app.globalData.templateIds
                if (itemSettings) {
                    for (let i = 0; i < tmplIds.length; i++) {
                        if (itemSettings[tmplIds[i]] != 'accept') {
                            tmplIds.pop();
                        }
                    }
                    if (tmplIds.length > 0) {
                        wx.requestSubscribeMessage({
                            tmplIds: tmplIds,
                            success(res) {
                                console.log("订阅成功通知")
                                console.log(res)
                            }
                        })
                    }
                } else {
                    wx.requestSubscribeMessage({
                        tmplIds: tmplIds,
                        success(res) {
                            console.log("订阅成功通知")
                            console.log(res)
                        }
                    })
                }
            }
        }
    })
}

module.exports = {
    formatTime: formatTime,
    formatDate: formatDate,
    parseDate: parseDate,
    addDay: addDay,
    addMonths: addMonths,
    subscriptionsSetting: subscriptionsSetting
}
